<?php

class MemberController extends Controller
{

    public function actionIndex()
    {
        $player = $this->AccountPlayerAll();
        $this->render('index',array("player" => $player));
    }

    public function actionSignup() {
        $this->render('signup');
    }

    public function actiongetAccDataAll(){
        $account = $_GET['account'];
        $acc_db = new AccountData;
        $count = $acc_db->model()->count("name = '".$account."' ");
        echo $count;
    }

    public function actionSign_up (){
        $account = $_GET['account'];
        $pass = $_GET['pass'];
        $email = $_GET['email'];
        $ip = $_GET['ip'];
        $acc_db = new AccountData;
        $acc_db->name = $account;
        $acc_db->password = base64_encode(sha1($pass, true));
        $acc_db->email = $email;
        $acc_db->last_ip = $ip;
        $acc_db->save();
        $auth = new UserIdentity($account,base64_encode(sha1($pass, true)));
        if ( $auth->auth() and UserIdentity::ERROR_NONE == $auth->errorCode ) {
            Yii::app()->user->login($auth);
        }
        echo 1;
    }

    public function actionSignIn(){
        $account = $_GET['account'];
        $pass = $_GET['pass'];
        $code = 0;
        $auth = new UserIdentity($account,base64_encode(sha1($pass, true)));
        if ( $auth->auth() and UserIdentity::ERROR_NONE == $auth->errorCode ) {
            $code = 1;
            Yii::app()->user->login($auth);
        }
        return $code;
    }

    public function actionSignOut() {
        Yii::app()->user->logout();
        echo "1";
    }

    public function AccountPlayerAll (){
        $userInfo = Yii::app()->user->getState("userInfo");
        $account = $userInfo['id'];
        $player = new Player;
        $player = $player->model()->findAll("account_id = '".$account."'");
        return CJSON::encode($player);
    }

}